package com.sinothk.gpserver.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.gpserver.modules.biz.entity.CommVo;
import com.sinothk.gpserver.modules.biz.entity.SearchUserEntity;
import com.sinothk.gpserver.modules.biz.entity.ShowInfo;
import com.sinothk.gpserver.modules.biz.entity.UserOperateInfo;
import com.sinothk.gpserver.modules.biz.mapper.CommMapper;
import com.sinothk.gpserver.modules.biz.service.ICommService;
import com.sinothk.gpserver.modules.biz.service.IInviteService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.gpserver.modules.system.entity.UserEntity;
import com.sinothk.gpserver.modules.system.service.IUserService;
import com.sinothk.gpserver.util.PageDataUtil;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

/**
 * <p>
 * 房间相关 服务实现类
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-08-30
 */
@Service
public class CommServiceImpl implements ICommService {
    @Resource
    private IInviteService inviteService;
    @Resource
    private CommMapper commMapper;
    @Autowired
    private IUserService userService;

    @Override
    public PageData<SearchUserEntity> searchUserByKeywords(PageVo<CommVo> pageVo) {
        Page<SearchUserEntity> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        IPage<SearchUserEntity> iPage = commMapper.searchUserByKeywords(page, pageVo.getData().getKeywords());
        return new PageDataUtil<SearchUserEntity>().create(iPage);
    }

    @Override
    public ShowInfo getShowInfo() {
        ShowInfo showInfo = new ShowInfo();
        showInfo.setInviteTips(inviteService.inviteTipsForUser());

        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        UserEntity user = userService.getByAccount(login.getAccount());
        if (user != null) {
            showInfo.setRoomCards(user.getRoomCards() == null ? 0 : user.getRoomCards());
        }

        return showInfo;
    }

    @Override
    public UserOperateInfo getUserOperateInfo() {
        Random random = new Random();
        UserOperateInfo userOperateInfo = new UserOperateInfo();
        userOperateInfo.setAllTimesNum(random.nextInt(100));
        userOperateInfo.setAllHandleNum(random.nextInt(70));
        userOperateInfo.setAllinNum(random.nextInt(20));
        return userOperateInfo;
    }
}
